﻿using MeetAI.Model.DTO;
using MeetAI.Service.IService;
using MeetAI.Utility;
using Nancy;
using Nancy.ModelBinding;
using System.Configuration;
using System.Linq;

namespace MeetAI.API.Modules
{
    public class productModule : NancyModule
    {
        private readonly IDy_Product_Service IDy_Product_Service;
        /// <summary>
        /// 静态资源 host
        /// </summary>
        private string staticDomain
        {
            get { return ConfigurationManager.AppSettings["staticDomain"].ToString() ?? ""; }
        }
        public productModule(IDy_Product_Service IDy_Product_Service) : base("/v1/product")
        {
            this.IDy_Product_Service = IDy_Product_Service;
            //获取首页 客片样片
            Get["/index/list", true] = async (param, token) =>
            {
                var dto = this.Bind<dy_product_dto>();
                Result res = new Result();
                if (dto == null)
                {
                    res.code = ResultType.fail;
                    res.message = "参数错误";
                    return Response.AsJson(res);
                }
                dto.order = "addtime desc";
                res.data = await IDy_Product_Service.getIndexProductAsync(dto);
                return Response.AsJson(res);
            };
            //获取列表
            Get["/list", true] = async (param, token) =>
             {
                 var dto = this.Bind<dy_product_dto>();
                 Result res = new Result();
                 if (dto == null)
                 {
                     res.code = ResultType.fail;
                     res.message = "参数错误";
                     return Response.AsJson(res);
                 }
                 dto.order = "addtime desc";
                 var lst = await IDy_Product_Service.getPageProductAsync(dto);
                 res.data = new
                 {
                     rows = lst,
                     total = lst.TotalCount,
                     pages = lst.TotalPages,
                     hasNext = lst.HasNextPage,
                     hasPrev = lst.HasPreviousPage
                 };
                 return Response.AsJson(res);
             };
            //获取明细
            Get["/detail", true] = async (param, token) =>
            {
                Result res = new Result();
                int? pid = Request.Query.pid;
                if (!pid.HasValue)
                {
                    res.code = ResultType.fail;
                    res.message = "参数错误";
                    return Response.AsJson(res);
                }
                var product = await IDy_Product_Service.FindAsync(s => s.id.Equals(pid));
                if (product == null)
                {
                    res.message = "商品不存在";
                    return Response.AsJson(res);
                }
                if (!string.IsNullOrWhiteSpace(product.photo))
                {
                    var photos = product.photo.Split("|,||-|".ToCharArray()).Select(s => !string.IsNullOrEmpty(s));
                }
                res.data = new
                {
                    product = new
                    {
                        product.title,
                        time = product.addtime,
                        litpic = string.Format("{0}{1}", staticDomain, product.litpic),
                        extitle = "遇见爱旅拍团队"
                    },
                    photos = string.IsNullOrEmpty(product.photo) ? null : product.photo.Split("|,||-|".ToCharArray()).Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => new { litpic = string.Format("{0}{1}", staticDomain, s) })
                };
                return Response.AsJson(res);
            };
        }
    }
}